package com.xmy.cultivate.controller;

import com.xmy.cultivate.entity.views.RenewClassRecord;
import com.xmy.cultivate.service.IRenewClassService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.DownloadUtils;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 续班表 前端控制器
 * </p>
 *
 * @author min
 * @since 2022-11-01
 */
@RestController
@RequestMapping("/api/renewClass")
@Scope("prototype")
public class RenewClassController extends BaseController{

    @Autowired
    IRenewClassService iRenewClassService;

    @GetMapping()
    public Result findAll(@RequestParam Map reMap,@RequestParam(value = "schoolIdList",defaultValue = "") String[] schoolIdList){
        /*Integer pageNum = StringUtils.isEmpty((String)reMap.get("pageNum")) ? 1 : Integer.valueOf((String)reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String)reMap.get("pageSize")) ? 10: Integer.valueOf((String)reMap.get("pageSize"));

        Map<String,Object> searchMap = new HashMap<>();
        Page<Course> page = new Page<>(pageNum,pageSize);*/

        //Long SchoolId = Long.parseLong(reMap.get("schoolId").toString());
        String SubjectsId = (String)reMap.get("subjectsId");
        Integer YearPart = Integer.parseInt(reMap.get("yearPart").toString());
        Integer QuarterNum = Integer.parseInt(reMap.get("quarterNum").toString());
        String TeacherName = (String) reMap.get("teacherName");
        String StartDate = (String) reMap.get("startDate");
        String EndDate = (String) reMap.get("endDate");
        if(schoolIdList.length == 0){
            //schoolIdList = baseSchoolIdStr.split(",");
        }

        Integer type = 1;
        if(reMap.get("type") != null){
            type = Integer.parseInt((String) reMap.get("type"));
        }

        List<RenewClassRecord> renewClassRecordList = iRenewClassService.getRenewRecord(schoolIdList,SubjectsId,YearPart,QuarterNum,TeacherName,StartDate,EndDate,type);


        return new Result(Code.OK,new PageResult(renewClassRecordList,(long)renewClassRecordList.size(),1L));
    }

    @GetMapping("exportData")
    public void exportData(@RequestParam Map reMap,@RequestParam(value = "schoolIdList",defaultValue = "") String[] schoolIdList) throws IOException {

        String SubjectsId = (String) reMap.get("subjectsId");
        Integer YearPart = Integer.parseInt(reMap.get("yearPart").toString());
        Integer QuarterNum = Integer.parseInt(reMap.get("quarterNum").toString());
        String TeacherName = (String) reMap.get("teacherName");
        String StartDate = (String) reMap.get("startDate");
        String EndDate = (String) reMap.get("endDate");
        if(schoolIdList.length == 0){
            schoolIdList = baseSchoolIdStr.split(",");
        }

        Integer type = 1;
        //1老师，2校区老师，3校长，4校区
        if(reMap.get("type") != null){
            type = Integer.parseInt((String) reMap.get("type"));
        }

        List<RenewClassRecord> renewClassRecordList = iRenewClassService.getRenewRecord(schoolIdList,SubjectsId,YearPart,QuarterNum,TeacherName,StartDate,EndDate,type);

        Workbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet();
        if(type.equals(1)){
            this.exportPerson(renewClassRecordList);
        }
        else if(type.equals(2)){
            this.exportSchoolPerson(renewClassRecordList);
        }else if(type.equals(3)){
            this.exportRector(renewClassRecordList);
        }else if(type.equals(4)){
            this.exportSchool(renewClassRecordList);
        }

    }

    /**
     * 个人
     * @param renewClassRecordList
     * @throws IOException
     */
    public void exportPerson(List<RenewClassRecord> renewClassRecordList) throws IOException {
        Workbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet();
        String [] titles = "任课老师,学生总人数,应续人数,已续人数,续班率".split(",");

        //处理标题
        Row row = sheet.createRow(0);
        int titleIndex=0;
        for (String title : titles) {
            Cell cell = row.createCell(titleIndex++);
            cell.setCellValue(title);
        }

        Cell cell= null;
        int rowIndex = 1;
        for (RenewClassRecord renewClassRecord:renewClassRecordList){
            row =sheet.createRow(rowIndex++);
            cell =row.createCell(0);
            cell.setCellValue(renewClassRecord.getTeacherName());
            cell =row.createCell(1);
            cell.setCellValue(renewClassRecord.getStudentAllCount());
            cell =row.createCell(2);
            cell.setCellValue(renewClassRecord.getShouldRenewCount());
            cell =row.createCell(3);
            cell.setCellValue(renewClassRecord.getAlreadyRenewCount());
            cell =row.createCell(4);
            cell.setCellValue(renewClassRecord.getRenewRate()+"%");
        }
        //3.完成下载
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        wb.write(os);
        new DownloadUtils().download(os,response,"个人续班率.xlsx");
    }

    /**
     * 校区任课老师
     * @param renewClassRecordList
     * @throws IOException
     */
    public void exportSchoolPerson(List<RenewClassRecord> renewClassRecordList) throws IOException {
        Workbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet();
        String [] titles = "校区,任课老师,学生总人数,应续人数,已续人数,续班率".split(",");

        //处理标题
        Row row = sheet.createRow(0);
        int titleIndex=0;
        for (String title : titles) {
            Cell cell = row.createCell(titleIndex++);
            cell.setCellValue(title);
        }

        Cell cell= null;
        int rowIndex = 1;
        for (RenewClassRecord renewClassRecord:renewClassRecordList){
            row =sheet.createRow(rowIndex++);
            cell =row.createCell(0);
            cell.setCellValue(renewClassRecord.getSchoolName());
            cell =row.createCell(1);
            cell.setCellValue(renewClassRecord.getTeacherName());
            cell =row.createCell(2);
            cell.setCellValue(renewClassRecord.getStudentAllCount());
            cell =row.createCell(3);
            cell.setCellValue(renewClassRecord.getShouldRenewCount());
            cell =row.createCell(4);
            cell.setCellValue(renewClassRecord.getAlreadyRenewCount());
            cell =row.createCell(5);
            cell.setCellValue(renewClassRecord.getRenewRate()+"%");
            cell =row.createCell(6);
            cell.setCellValue(renewClassRecord.getRenewRate()+"%");
        }
        //3.完成下载
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        wb.write(os);
        new DownloadUtils().download(os,response,"校区老师续班率.xlsx");
    }

    /**
     * 校长
     * @param renewClassRecordList
     * @throws IOException
     */
    public void exportRector(List<RenewClassRecord> renewClassRecordList) throws IOException {
        Workbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet();
        String [] titles = "校区,校长,学生总人数,应续人数,已续人数,续班率".split(",");

        //处理标题
        Row row = sheet.createRow(0);
        int titleIndex=0;
        for (String title : titles) {
            Cell cell = row.createCell(titleIndex++);
            cell.setCellValue(title);
        }

        Cell cell= null;
        int rowIndex = 1;
        for (RenewClassRecord renewClassRecord:renewClassRecordList){
            row =sheet.createRow(rowIndex++);
            cell =row.createCell(0);
            cell.setCellValue(renewClassRecord.getSchoolName());
            cell =row.createCell(1);
            cell.setCellValue(renewClassRecord.getTeacherName());
            cell =row.createCell(2);
            cell.setCellValue(renewClassRecord.getStudentAllCount());
            cell =row.createCell(3);
            cell.setCellValue(renewClassRecord.getShouldRenewCount());
            cell =row.createCell(4);
            cell.setCellValue(renewClassRecord.getAlreadyRenewCount());
            cell =row.createCell(5);
            cell.setCellValue(renewClassRecord.getRenewRate()+"%");
        }
        //3.完成下载
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        wb.write(os);
        new DownloadUtils().download(os,response,"校长续班率.xlsx");
    }

    /**
     * 校区
     * @param renewClassRecordList
     * @throws IOException
     */
    public void exportSchool(List<RenewClassRecord> renewClassRecordList) throws IOException {
        Workbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet();
        String [] titles = "校区,学生总人数,应续人数,已续人数,续班率".split(",");

        //处理标题
        Row row = sheet.createRow(0);
        int titleIndex=0;
        for (String title : titles) {
            Cell cell = row.createCell(titleIndex++);
            cell.setCellValue(title);
        }

        Cell cell= null;
        int rowIndex = 1;
        for (RenewClassRecord renewClassRecord:renewClassRecordList){
            row =sheet.createRow(rowIndex++);
            cell =row.createCell(0);
            cell.setCellValue(renewClassRecord.getSchoolName());
            cell =row.createCell(1);
            cell.setCellValue(renewClassRecord.getStudentAllCount());
            cell =row.createCell(2);
            cell.setCellValue(renewClassRecord.getShouldRenewCount());
            cell =row.createCell(3);
            cell.setCellValue(renewClassRecord.getAlreadyRenewCount());
            cell =row.createCell(4);
            cell.setCellValue(renewClassRecord.getRenewRate()+"%");
        }
        //3.完成下载
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        wb.write(os);
        new DownloadUtils().download(os,response,"校区续班率.xlsx");
    }


    /**
     * 交费率
     * @return
     */
    @GetMapping("/payFee")
    public Result payFee(@RequestParam Map reMap){
        String subjectsId = (String)reMap.get("subjectsId");
        Integer quarterNum = 1;

        Integer type = 1;
        if(reMap.get("type") != null){
            type = Integer.parseInt((String) reMap.get("type"));
        }

        List<RenewClassRecord> renewClassRecordList = iRenewClassService.getPayFee(subjectsId,type,quarterNum);

        return new Result(Code.OK,new PageResult(renewClassRecordList,(long)renewClassRecordList.size(),1L));
    }

}
